package cn.edu.stu.syllabus.oa.list;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import javax.inject.Inject;

import butterknife.BindView;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.base.BaseActivity;
import cn.edu.stu.syllabus.repository.oa.OaSearchInfo;
import cn.edu.stu.syllabus.dialog.SearchOaDialog;
import cn.edu.stu.syllabus.datas.OaCompany;
import cn.edu.stu.syllabus.oa.star.OaStarActivity;
import cn.edu.stu.syllabus.utils.ActivityUtils;
import cn.edu.stu.syllabus.utils.GuideHelper;

public class OaActivity extends BaseActivity implements SearchOaDialog.OnSearchButtonClickListener, Toolbar.OnMenuItemClickListener {

    @Inject
    OaPresenter mOaPresenter;

    private OaSearchInfo mOaSearchInfo;

    private String lastSearchKeyWord;
    private int lastSearchPosition;

    @BindView(R.id.oaSearchFab)
    FloatingActionButton mOaSearchFab;

    AlertDialog mClearReadDialog;

    public interface OnClearEnsureListener {
        void onClick();
    }

    OnClearEnsureListener mOnClearEnsureListener;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_oa;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setTitle("办公自动化");

        mToolbar.inflateMenu(R.menu.menu_oa);
        mToolbar.setOnMenuItemClickListener(this);

        OaFragment oaFragment = (OaFragment) getSupportFragmentManager()
                .findFragmentById(R.id.contentFrame);
        if (oaFragment == null) {
            oaFragment = OaFragment.newInstance();
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager()
                    , oaFragment, R.id.contentFrame);
        }
        setOnToolbarDoubleClickListener(oaFragment);

        mOaSearchInfo = new OaSearchInfo();
        lastSearchKeyWord = "";
        lastSearchPosition = 0;

        mOaSearchFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final SearchOaDialog searchOaDialog = SearchOaDialog
                        .newInstance(lastSearchKeyWord, lastSearchPosition);
                searchOaDialog.show(getSupportFragmentManager(),
                        SearchOaDialog.DIALOG_SEARCH_OA);
                searchOaDialog.setOnSearchButtonClick(OaActivity.this);
            }
        });

        mClearReadDialog = new AlertDialog.Builder(this)
                .setMessage("确认清除所有浏览信息？")
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (!OaActivity.this.isDestroyed() && mOnClearEnsureListener != null) {
                            mOnClearEnsureListener.onClick();
                        }
                    }
                })
                .setNegativeButton("取消", null)
                .create();

        mOnClearEnsureListener = oaFragment;


        DaggerOaComponent.builder()
                .oaPresenterModule(new OaPresenterModule(oaFragment, mOaSearchInfo))
                .build().inject(this);


        GuideHelper.startActSeqNow(this,
                GuideHelper.getActMainTapTarget(R.drawable.ic_oa, this,
                        "这是办公自动化主界面",
                        "顶部下拉可以进行刷新\n底部上拉可以加载更多\n双击Title可以立刻回到顶部")
                        .id(1),
                GuideHelper.getToolbarItemTapTarget(mToolbar, R.id.action_fav_oa,
                        "这是打开收藏夹的按钮",
                        "点击后, 你可以查看加入收藏夹的办公自动化信息")
                        .id(2),
                GuideHelper.getToolbarItemTapTarget(mToolbar, R.id.action_clear,
                        "这是清除浏览记录的按钮",
                        "点击后, 你清除所有的办公自动化的浏览信息")
                        .id(3),
                GuideHelper.getFabTapTarget(mOaSearchFab,
                        "这是搜索办公自动化信息的按钮",
                        "点击后, 你可以根据关键字和部分搜索特定的办公自动化信息")
                        .id(4)
        );

    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_fav_oa) {
            Intent intent = new Intent(this, OaStarActivity.class);
            startActivity(intent);
        } else if (id == R.id.action_clear) {
            mClearReadDialog.show();
        }
        return true;
    }

    @Override
    public void onSearchButtonClick(String keyword, int position) {
        lastSearchKeyWord = keyword;
        lastSearchPosition = position;
        OaSearchInfo oaSearchInfo = new OaSearchInfo();
        oaSearchInfo.setKeyword(keyword);
        oaSearchInfo.setSubCompanyId(OaCompany.getOaCompanies().get(position).getId());

        Intent intent = OaSearchResultActivity.newIntent(this, oaSearchInfo);
        startActivity(intent);
    }

}
